<style type="text/css">
.sizetable th,.sizetable td{padding:3px 5px !important;text-align:center;}
</style>
    <div>
        <form  class="layui-form layui-form-pane fixed-bar-form" action="{:url('')}" method="post" id="hisi-table-search">
       <div>
       {if $action eq 'scan_code'}
         <table width="750" border="0" cellspacing="0">
           <tr>
             <td width="500px" valign="top">
             <table class="layui-table" style="width:510px">
                <thead>
                    <tr>
                      <th><div align="right">服装条码:</div></th>
                      <th align="left"><input type="text" name="dress_line_code" id="dress_line_code" lay-verify="" placeholder="" class="layui-input layui-inline" style="display:inline;"></th>
                    </tr>
                    <tr>
                        <th><div align="right">订单号*：</div></th>
                        <th align="left"><input type="text" name="client_ordernum" id="client_ordernum" value="" tabindex="100" lay-verify="required" autocomplete="off" class="layui-input">
                        <input type="hidden" name="sku" id="sku">
                        <input type="hidden" name="factory_id[]" id="factory_input">
                        <div id="errtag"></div></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><div align="right"><strong>尺码库存*：</strong></div></td>
                        <td><div id="sizeblock"><span style="color:#999999">请先输入订单号</span></div></td>
                  </tr>
                    <tr>
                      <td><div align="right">货架号：</div></td>
                      <td><input type="text" name="stock_position[0]" value="" tabindex="100" id="stock_position" autocomplete="off" class="layui-input"></td>
                    </tr>
                    <tr>
                      <td><div align="right">备注：</div></td>
                      <td><input type="text" name="remark[0]" id="remark" value="" tabindex="100"  autocomplete="off" class="layui-input"></td>
                  </tr>
                </tbody>
         </table>
         <div align="center">

            <button type="submit" id="editsubmitbtn" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="demo1" style="width:180px">提 交</button>
         </div>
            <input type="hidden" class="field-id" name="save_type" value="add">
             </td>
             <td valign="top"><div style="margin:8px 2px;padding:10px 8px;background-color:#f2f2f2;height:300px;font-size:14px;">处理结果：
             	<div id="action_result"></div>
             </div></td>
           </tr>
         </table>
		{else /}
		{if $action eq 'addstock'}
            <table class="layui-table" style="width:480px">
                <thead>
                    <tr>
                      <th><div align="right">成衣SKU*:</div></th>
                      <th align="left"><input type="text" name="sku" id="sku" lay-verify="" placeholder="输入SKU" class="layui-input layui-inline" style="width:180px;display:inline;"> <a href="#" id="search_size">[查询尺码]</a><div id="errtag"></div></th>
                    </tr>
                    <tr>
                        <th><div align="right">所属工厂*：</div></th>
                        <th>{:Form::select('factory_id[]', '', $factoryKV, '', '')}</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><div align="right"><strong>尺码库存*：</strong></div></td>
                        <td><div id="sizeblock"><span style="color:#999999">显示尺码区域，请先输入sku</span></div></td>
                  </tr>
                    <tr>
                      <td><div align="right">货架号*：</div></td>
                      <td><input type="text" name="stock_position[0]" value="" tabindex="100" lay-verify="required" autocomplete="off" class="layui-input"></td>
                    </tr>
                    <tr>
                      <td><div align="right">备注：</div></td>
                      <td><textarea name="remark[0]" cols="3" class="layui-textarea" style="height:50px;min-height:50px;"></textarea></td>
                  </tr>
                </tbody>
            </table>
            <input type="hidden" class="field-id" name="save_type" value="add">
		{else /}
        	<table class="layui-table" style="width:100%" id="demo1" lay-filter="demo1">
                <thead>
                    <tr>
                        <th>所属工厂/订单</th>
                        <th>尺码库存调整</th>
                        <th>货架号</th>
                        <th>包装条码号(skc)</th>
                        <th>备注</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach $stock_list as $k => $v}
                    <tr>
                        <td>{$v['factory']}<input type="hidden" class="field-id" name="factory_id[]" value="{$v['factory_id']}">
                            <input type="hidden" class="size_name_class" name="edit_client_ordernum[]" value="{$v['client_ordernum']}">
                        <input type="hidden" class="size_name_class" name="size_name[]" value="{$v['size_name']}"><br />
						{$v['client_ordernum']}
                        </td>
                        <td style="padding:3px 10px;">
                            {if !empty($v['available_detail_arr'])}
                            <table class="sizetable"><tr><td>&nbsp;</td>
                            {foreach $v['available_detail_arr'] as $sizename => $sizeinfo}
                                <td>{$sizename}</td>	
                            {/foreach}
                            </tr><tr><td>正品</td>
                            {foreach $v['available_detail_arr'] as $sizename => $sizeinfo}
                                <td><input type="text" name="available[{$k}][{$sizename}]" value="{if !empty($v['available_detail'])}{$sizeinfo}{else /}0{/if}" style="width:40px;"></td>	
                            {/foreach}
                            </tr>
                            <tr><td>次品</td>
                            {foreach $v['defective_detail_arr'] as $sizename => $sizeinfo}
                                <td><input type="text" name="defective[{$k}][{$sizename}]" value="{if !empty($v['defective_detail'])}{$sizeinfo}{else /}0{/if}" style="width:40px;"></td>	
                            {/foreach}
                            </table>
                            {/if}
                        </td>
                      <td><input type="text" name="stock_position[{$k}]" value="{$v['stock_position']}" tabindex="100" autocomplete="off" style="width:100px;"></td>
                      <td><input type="text" name="dress_line_code[{$k}]" value="{$v['dress_line_code']}" tabindex="100" autocomplete="off" style="width:160px;"></td>
                  <td><textarea name="remark[{$k}]" cols="3"  style="width:100px;">{$v['remark']}</textarea></td>
                    </tr>
                    {/foreach}
                </tbody>
            </table>
            <input type="hidden" class="field-id" name="sku" value="{$sku}">
        {/if}
                <div class="pop-bottom-bar">
            <button type="submit" id="editsubmitbtn" class="layui-btn"  btn_name="edit_one" lay-submit="" lay-filter="demo1">提交保存</button>
            <a href="javascript:parent.layui.layer.closeAll();" class="layui-btn layui-btn-primary ml10">取消</a>
        </div>
        {/if}
       </div>

        </form>
    </div>
<table id="dataTable"></table>
{include file="system@block/layui" /}
<script>
	layui.use(['tool','laydate','table','form'], function() {
		var element = layui.element;
		var layer = layui.layer;
		var $ = layui.jquery;
		var form = layui.form;
		var table = layui.table;
		function doSaveStock(data){
			$.ajax({
			   type: "POST",
			   url: "{:url('mp/SkuStock/StockEdit?action=dosave')}",
			   data: data,
			   dataType:"json",
			   success: function(res){
					var data=res.data;
					if(res.code==1){
						if($("#editsubmitbtn").attr("btn_name")=='edit_one'){
							$("#editsubmitbtn").html("保存成功");
							setTimeout(function(){
								parent.layui.layer.closeAll();
								//parent.layui.table.reload();
							},2000);
							layer.msg('修改成功', {icon: 1,anim: 6});
							return;
						}
						var ret_msg="订单号："+data.client_ordernum+"<br>sku:"+data.sku+"<br>工厂："+data.factory_name+"<br>库存："
						+data.available_count+"<br>库存详细："+data.available_detail+"<br>次品： "+data.defective_count+"<br>"+data.defective_detail+"<br>处理成功 "+data.date;
						$("#action_result").html(ret_msg);
						$("#dress_line_code").val('');
						$("#client_ordernum").val('');
						$("#stock_position").val('');
						$("#remark").val('');
						$("#factory_input").val('');
						$("#dress_line_code").focus();
					}else{
						alert(res.msg);
						$("#action_result").html(res.msg);
					}
					
			   }
			});
		}

		form.on('submit(demo1)', function (data) {
			//表单数据formData
			var formData;
			formData = data.field;
			doSaveStock(formData)
		})
		$("#search_size").click(function(){
			if($("#sku").val()==''){
				alert('请先填写入库的sku');
			}else{
				//getSkuSize();	
			}
		});
		$("#client_ordernum").bind('input propertychange', function() {
			var ordernumber=$("#client_ordernum").val();
			if(ordernumber.length>=7){
				getSkuSize('client_ordernum');
			}
		});
		$("#dress_line_code").bind('input propertychange', function() {
			var dress_line_code=$("#dress_line_code").val();
			if(dress_line_code.length>=17){
				$("#client_ordernum").focus();
				getSkuSize('dress_line_code');
			}
		});
		function getSkuSize(searchtype){
			var dress_line_code=$("#dress_line_code").val();
			var ordernumber=$("#client_ordernum").val();
			var sku=$("#sku").val();
			//if(sku=='') return;
			$.ajax({
			   type: "POST",
			   url: "{:url('mp/SkuStock/StockEdit?action=get_sku_size')}",
			   data: {sku:sku,client_ordernum:ordernumber,dress_line_code:dress_line_code},
			   dataType:"json",
			   success: function(res){
			   		var data=res.data;
					$("#errtag").empty();
					if(res.msg!='success'){
						if(searchtype=='client_ordernum'){
							$("#errtag").html(res.msg);
						}
						return;
					}
					if(searchtype=='dress_line_code'){
						if(typeof(data.sizeval)!=="undefined"){
							$("#client_ordernum").val(data.sizeval.client_ordernum);
							$("#stock_position").val(data.sizeval.stock_position);
							$("#remark").val(data.sizeval.remark);
						}
					}
					$("#sku").val(data.sku);$("#factory_input").val(data.factory);
			   		var sizehtml='<table class="sizetable" style="width:100%"><tr><th style="width:40px;"></th>'
					for(var i=0;i<data.ps_size_name.length;i++){
						sizehtml+='<th style="width:40px;">'+data.ps_size_name[i]+'</th>';
					} 
					sizehtml+='</tr><tr><td>正品</td>';
					var av_stock=0,de_stock=0;
					if(typeof(data.sizeval)!=="undefined"){
						av_stock=eval(data.sizeval.av);
						de_stock=eval(data.sizeval.de);
					}
					for(var i=0;i<data.ps_size_name.length;i++){
						var av_size_stock=typeof(av_stock[data.ps_size_name[i]])==="undefined"? 0 : av_stock[data.ps_size_name[i]] ;
						sizehtml+='<th><input type="text" name="available[0]['+data.ps_size_name[i]+
						']" class="firstsizeinput" lay-verify="required|number" value="'+av_size_stock+'" style="width:40px;"></th>';
					} 
					sizehtml+='</tr><tr><td>次品</td>';
					for(var i=0;i<data.ps_size_name.length;i++){
						var de_size_stock=typeof(de_stock[data.ps_size_name[i]])==="undefined"? 0 : de_stock[data.ps_size_name[i]] ;
						sizehtml+='<th><input type="text" name="defective[0]['+data.ps_size_name[i]+']" value="'+de_size_stock+'" style="width:40px;"></th>';
					}
					sizehtml+='</tr></table>';
					$("#sizeblock").html(sizehtml);
					if(typeof(data.sizeval)!=="undefined"){
						$("#client_ordernum").val(data.sizeval.client_ordernum);
						$("#stock_position").val(data.sizeval.stock_position);
						$("#remark").val(data.sizeval.remark);
					}
					$(".firstsizeinput").eq(0).select();
			   }
			});
		}
 		$("#dress_line_code").focus();
	});
</script>